package com.ayoka.ui.course.detail;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ayoka.R;
import com.ayoka.adapter.CourseDetailMessageOptionAdapter;
import com.ayoka.bean.CourseBean;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.dialog.BaseDialogBuilder;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.utils.UIUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class CourseDetailMessageOptionDialog extends BaseDialogFragment {

    public static CourseDetailMessageOptionDialog newInstance(Builder builder) {
        CourseDetailMessageOptionDialog dialog = new CourseDetailMessageOptionDialog();
        Bundle bundle = getArgumentBundle(builder);
        bundle.putString("Title", builder.title);
        bundle.putSerializable("Options", (Serializable) builder.options);
        bundle.putLong("Duration", builder.duration);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        TextView tvTitle = container.findViewById(R.id.tv_option_title);
        if (getArguments() != null) {
            tvTitle.setText(getArguments().getString("Title"));
        }
        if (getArguments() != null && getArguments().getSerializable("Options") != null) {
            List<CourseBean.OptionBean> options = (List<CourseBean.OptionBean>) getArguments().getSerializable("Options");
            RecyclerView recyclerViewOption = container.findViewById(R.id.recyclerView_option);
            recyclerViewOption.setLayoutManager(new GridLayoutManager(getContext(), 3));
            final CourseDetailMessageOptionAdapter messageOptionAdapter = new CourseDetailMessageOptionAdapter(options);
            messageOptionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    ((CourseDetailMessageOptionAdapter)adapter).setSingleChoice(position);
                    CourseBean.OptionBean optionBean = (CourseBean.OptionBean) adapter.getItem(position);
                    if (mDialogResultListener != null) {
                        mDialogResultListener.result(CourseDetailMessageOptionDialog.this, optionBean);
                    }
                }
            });
            recyclerViewOption.setAdapter(messageOptionAdapter);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        long duration = 10;
        if (getArguments() != null && getArguments().getLong("Duration") != 0) {
            duration = getArguments().getLong("Duration");
        }
        UIUtils.postTaskDelay(Task, duration * 1000);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_course_detail_message_option;
    }


    public static class Builder extends BaseDialogBuilder<Builder, CourseDetailMessageOptionDialog> {

        private String title;
        private List<CourseBean.OptionBean> options;
        private long duration;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setOptionData(List<CourseBean.OptionBean> options) {
            this.options = options;
            return this;
        }

        @Override
        public CourseDetailMessageOptionDialog build() {
            return CourseDetailMessageOptionDialog.newInstance(this);
        }

        public Builder setDuration(long duration) {
            this.duration = duration;
            return this;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        UIUtils.removeTask(Task);
    }

    private Runnable Task = new Runnable() {
        @Override
        public void run() {
            try {
                dismiss();
            } catch (Exception e){
                e.printStackTrace();
            }

        }
    };
}